﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public class DutyDAL : IDutyDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public DutyDAL()
        {

        }
        public IList<DutyInfo> ListDuties(DutyInfo dInfo)
        {
            string strSQL;
            
            List<DutyInfo> infoList = new List<DutyInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Sequence", dInfo.Sequence);

                strSQL = "SELECT DUTY_SEQ, DUTY_NAME, REMARKS, CREATED_BY, UPDATED_BY"
                    + " FROM HRM_MP_DUTIES"
                    + " WHERE RECORD_STATUS not like @DeleteRecord"
                    + (dInfo.Sequence > 0 ? " AND DUTY_SEQ = @Sequence" : "");

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    DutyInfo Info = new DutyInfo(rdr.GetInt32(0)
                            , rdr.IsDBNull(1) ? "" : rdr.GetString(1)
                            , rdr.IsDBNull(2) ? "" : rdr.GetString(2)
                            , rdr.IsDBNull(3) ? (int?)null : rdr.GetInt32(3)
                            , rdr.IsDBNull(4) ? (int?)null : rdr.GetInt32(4));
                    infoList.Add(Info);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
            return infoList;
        }

        //public DutyInfo GetDutyInfo(Nullable<int> Seq)
        //{
        //    DutyInfo dutyInfo = new DutyInfo();
        //    string strSQL;

        //    try
        //    {
        //        dbManager.Open();
        //        dbManager.CreateParameters(2);

        //        dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
        //        dbManager.AddParameters(1, "@Seq", Seq);

        //        strSQL = "SELECT DUTY_SEQ, DUTY_NAME, REMARKS, CREATED_BY, UPDATED_BY"
        //            + " FROM HRM_MP_DUTIES"
        //            + " WHERE RECORD_STATUS not like @DeleteRecord"
        //                + " AND DUTY_SEQ = @Seq";

        //        IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

        //        while (rdr.Read())
        //        {                    
        //            dutyInfo.DutySeq = rdr.IsDBNull(0) ? (int?)null : rdr.GetInt32(0);
        //            dutyInfo.DutyName = rdr.IsDBNull(1) ? "" : rdr.GetString(1);
        //            dutyInfo.Remarks = rdr.IsDBNull(2) ? "" : rdr.GetString(2);
        //        }

        //        return dutyInfo;
        //    }
        //    catch
        //    {
        //        return null;
        //        throw;
        //    }
        //    finally
        //    {
        //        dbManager.CloseReader();
        //        dbManager.Dispose();
        //    }
        //}

        public void Insert(DutyInfo dutyInfo)
        {
             string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(5);

                dbManager.AddParameters(0, "@DutyName", dutyInfo.Name);
                dbManager.AddParameters(1, "@Remarks", dutyInfo.Remarks);
                dbManager.AddParameters(2, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(3, "@CreatedBy", dutyInfo.CreatedBy);
                dbManager.AddParameters(4, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO HRM_MP_DUTIES(DUTY_NAME, REMARKS"
                        + ", RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@DutyName, @Remarks, @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Update(DutyInfo dutyInfo)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(5);

                dbManager.AddParameters(0, "@DutySeq", dutyInfo.Sequence);
                dbManager.AddParameters(1, "@DutyName", dutyInfo.Name);
                dbManager.AddParameters(2, "@Remarks", dutyInfo.Remarks);
                dbManager.AddParameters(3, "@UpdatedBy", dutyInfo.UpdatedBy);
                dbManager.AddParameters(4, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_MP_DUTIES"
                           + " SET DUTY_NAME = @DutyName"
                                + ", REMARKS = @Remarks"
                                + ", UPDATED_BY = @UpdatedBy"
                                + ", UPDATED_WHEN = @UpdatedWhen"
                           + " WHERE DUTY_SEQ = @DutySeq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Delete(int Sequence, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Seq", Sequence);
                dbManager.AddParameters(1, "@RecordStatus", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(3, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_MP_DUTIES"
                    + " SET RECORD_STATUS = @RecordStatus"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE DUTY_SEQ = @Seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }        
    }
}
